package cheesymock;

import net.sf.cglib.proxy.*;

/**
 * Factory object that generates new proxies using CGLIB, which means
 * that we can also create objects for regular classes (both abstract
 * and normal ones).
 *
 * @author fredrik
 */
public class CGLIBProxyFactory implements ProxyFactory {
	
	public Object newProxy(Class<?> clazz, Validator[] validators, Object delegate, DefaultReturn defaultReturn) {
		CGLIBMock mock = new CGLIBMock(clazz, validators, delegate, defaultReturn);
		Enhancer e = new Enhancer();
		e.setSuperclass(clazz);
		e.setCallback(mock);
		return e.create();
	}

	public Object getHandler(Object proxy) {
		return ((Factory) proxy).getCallback(0);
	}
}